package com.yunsc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.exception.YunscException;
import com.yunsc.system.dao.YunscStoreRoleDao;
import com.yunsc.system.dto.StoreRoleDTO;
import com.yunsc.system.entity.YunscStoreRoleEntity;
import com.yunsc.system.entity.YunscStoreRoleMenuEntity;
import com.yunsc.system.service.YunscStoreRoleMenuService;
import com.yunsc.system.service.YunscStoreRoleService;
import com.yunsc.system.vo.StoreRoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("yunscStoreRoleService")
public class YunscStoreRoleServiceImpl extends ServiceImpl<YunscStoreRoleDao, YunscStoreRoleEntity> implements YunscStoreRoleService {

    @Resource
    private YunscStoreRoleMenuService storeRoleMenuService;

    @Override
    public List<StoreRoleVO> queryStoreRoleList(Integer storeId) {
        List<StoreRoleVO> storeRoleList = this.baseMapper.queryStoreRoleList(storeId);

        return storeRoleList.stream().filter(role -> role.getParentId() == 0).map(role -> {
            role.setChildren(getRoleChildren(role.getRoleId(), storeRoleList));
            return role;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void editStoreRole(Integer storeId, StoreRoleDTO storeRoleDTO) {
        List<Integer> menus = storeRoleDTO.getForm().getMenus();
        Integer roleId = storeRoleDTO.getRoleId();
        // 1、更新角色信息
        YunscStoreRoleEntity storeRoleEntity = this.getById(roleId);
        if (storeRoleEntity == null) {
            throw new YunscException("角色不存在");
        }
        BeanUtils.copyProperties(storeRoleDTO.getForm(), storeRoleEntity);
        storeRoleEntity.setStoreId(storeId);
        boolean update = this.updateById(storeRoleEntity);
        if (!update) {
            throw new YunscException("更新角色失败");
        }
        // 2、更新角色菜单关系
        if (!CollectionUtils.isEmpty(menus)) {
            // 删除原有角色菜单关系
            QueryWrapper<YunscStoreRoleMenuEntity> wrapper = new QueryWrapper<YunscStoreRoleMenuEntity>()
                    .eq("role_id", roleId)
                    .eq("store_id", storeId);
            storeRoleMenuService.remove(wrapper);
            // 新增角色菜单关系
            List<YunscStoreRoleMenuEntity> storeRoleEntities = new ArrayList<>();
            for (Integer menuId : menus) {
                YunscStoreRoleMenuEntity storeRoleMenuEntity = new YunscStoreRoleMenuEntity();
                storeRoleMenuEntity.setRoleId(roleId);
                storeRoleMenuEntity.setMenuId(menuId);
                storeRoleMenuEntity.setStoreId(storeId);
                storeRoleEntities.add(storeRoleMenuEntity);
            }
            storeRoleMenuService.saveBatch(storeRoleEntities);
        }
    }

    @Override
    @Transactional
    public void deleteStoreRole(Integer roleId) {
        // 1、删除角色信息
        boolean update = this.removeById(roleId);
        if (!update) {
            throw new YunscException("删除角色失败");
        }
        // 2、删除角色菜单关系
        QueryWrapper<YunscStoreRoleMenuEntity> wrapper = new QueryWrapper<YunscStoreRoleMenuEntity>()
                .eq("role_id", roleId);
        boolean remove = storeRoleMenuService.remove(wrapper);
        if (!remove) {
            throw new YunscException("删除角色菜单关系失败");
        }

    }

    //  新增角色
    @Override
    public void addStoreRole(Integer storeId, StoreRoleDTO storeRoleDTO) {
        YunscStoreRoleEntity roleEntity = new YunscStoreRoleEntity();
        BeanUtils.copyProperties(storeRoleDTO.getForm(), roleEntity);
        roleEntity.setStoreId(storeId);
        boolean save = this.save(roleEntity);
        if (!save) {
            throw new YunscException("新增角色失败");
        }
        // 新增角色菜单关系
        List<Integer> menus = storeRoleDTO.getForm().getMenus();
        if (!CollectionUtils.isEmpty(menus)) {
            List<YunscStoreRoleMenuEntity> storeRoleEntities = new ArrayList<>();
            for (Integer menuId : menus) {
                YunscStoreRoleMenuEntity storeRoleMenuEntity = new YunscStoreRoleMenuEntity();
                storeRoleMenuEntity.setRoleId(roleEntity.getRoleId());
                storeRoleMenuEntity.setMenuId(menuId);
                storeRoleMenuEntity.setStoreId(storeId);
                storeRoleEntities.add(storeRoleMenuEntity);
            }
            storeRoleMenuService.saveBatch(storeRoleEntities);
        }

    }

    // 递归获取角色子节点
    private List<YunscStoreRoleEntity> getRoleChildren(Integer roleId, List<StoreRoleVO> storeRoleList) {
        List<YunscStoreRoleEntity> children = new ArrayList<>();
        for (StoreRoleVO storeRoleVO : storeRoleList) {
            if (storeRoleVO.getParentId().equals(roleId)) {
                children.add(storeRoleVO);
            }
        }
        return children;
    }
}